package chaussitag.storagespy;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	private static final String TAG = "MainActivity";

	private Button mStartButton;
	private Button mStopButton;
	private ProgressDialog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mStartButton = (Button) findViewById(R.id.start_observe);
		mStartButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startSdcardObserveService();
				mStartButton.setEnabled(false);
			}
			
		});

		mStopButton = (Button) findViewById(R.id.stop_observe);
		mStopButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				stopSdcardObserveService();
				mStartButton.setEnabled(true);
				mStopButton.setEnabled(false);
				new AlertDialog.Builder(MainActivity.this)
						.setTitle(R.string.observing_stopped_dialog_title)
						.setMessage(R.string.observing_stopped_dialog_message)
						.setPositiveButton(getResources().getText(android.R.string.ok),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										dialog.dismiss();
									}
							
						})
						.create().show();
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();
		Intent intent = new Intent(this, SdcardObserverService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	private LocalBinder mService;
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.d(TAG, "OnServiceConnected");
			mService = (LocalBinder) service;
			initButtons();
			unbindService(mConnection);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mService = null;
		}
	};

	private void initButtons() {
		Log.d(TAG, "enableButtons");
		if (mService != null) {
			boolean observing = mService.isObserving();
			Log.d(TAG, "observing " + observing);
			mStartButton.setEnabled(!observing);
			mStopButton.setEnabled(observing);
		}
	}

	BroadcastReceiver mObserveFinishedReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "broadcat received " + intent.getAction());
			onSdcardObserveFinished();
		}
	};

	private void onSdcardObserveFinished() {
		unregisterReceiver(mObserveFinishedReceiver);

		mLoadingDialog.dismiss();

		AlertDialog.Builder builder =
			new AlertDialog.Builder(this).
				setTitle(R.string.observing_prepared_dialog_title).
				setMessage(getResources().getText(R.string.observing_prepared_dialog_message)).
				setPositiveButton(getResources().getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					MainActivity.this.finish();
				}
			});
		builder.create().show();
	}

	private String mSdcardObservePrepared = "androidsdklearn.sdcardobserver.action.SDCARDOBSERVEPREPARED";
	private void startSdcardObserveService() {

		Intent broadcastIntent = new Intent(mSdcardObservePrepared);
		PendingIntent pendingIntent =
			PendingIntent.getBroadcast(this, 0, broadcastIntent, 0);

		registerReceiver(mObserveFinishedReceiver, new IntentFilter(mSdcardObservePrepared));

		Bundle extras = new Bundle();
		extras.putParcelable("broadcast", pendingIntent);
		Intent serviceIntent = new Intent(this, SdcardObserverService.class);
		serviceIntent.putExtras(extras);
		startService(serviceIntent);

		startLoadingAnimation();
	}

	private void startLoadingAnimation() {
		Resources resources = getResources();
		mLoadingDialog = ProgressDialog.show(this,
				resources.getText(R.string.observing_progress_bar_title),
				resources.getText(R.string.observing_progress_bar_message));
	}

	private void stopSdcardObserveService() {
		Intent intent = new Intent(this, SdcardObserverService.class);
		stopService(intent);
	}
}
